package com.zkzx.freight.bill.service;


import com.zkzx.common.freight.command.OrderCommand;
import com.zkzx.common.freight.model.OrderDeliveryVO;
import com.zkzx.freight.bill.mapper.OrderDeliveryMapper;
import com.zkzx.freight.bill.mapper.entity.OrderDelivery;
import com.zkzx.freight.bill.mapper.entity.OrderDeliveryExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;

/**
 * 北京中科智兴物联科技有限公司 @版权所有
 *
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/12/18:13
 * @Description:
 */
@Slf4j
@Service
public class OrderDeliveryService {

    @Resource
    private OrderDeliveryMapper orderDeliveryMapper;

    /**
     * 根据订单id查询发货方信息收货信息
     * @param orderId 订单id
     * @return 发货方信息收货信息
     */
    public OrderDeliveryVO findVOByOrderIdId(Integer orderId){
        OrderDelivery orderDelivery = findByOrderId(orderId);
        if (Objects.isNull(orderDelivery)){
            return null;
        }
       return OrderDeliveryVO.builder()
               .id(orderDelivery.getId())
               .orderId(orderDelivery.getOrderid())
               .shipperCode(orderDelivery.getShippercode())
               .shipperName(orderDelivery.getShippername())
               .shipperContact(orderDelivery.getShippercontact())
               .loadAddress(orderDelivery.getLoadaddress())
               .loadDetailedAddress(orderDelivery.getLoaddetailedaddress())
               .loadTime(orderDelivery.getLoadtime())
               .loadLongitude(orderDelivery.getLoadlongitude())
               .loadLatitude(orderDelivery.getLoadlatitude())
               .loadProvince(orderDelivery.getLoadprovince())
               .loadCity(orderDelivery.getLoadcity())
               .loadCounty(orderDelivery.getLoadcounty())
               .consigneeCode(orderDelivery.getConsigneecode())
               .consigneeName(orderDelivery.getConsigneename())
               .consigneeContact(orderDelivery.getConsigneecontact())
               .consigneeAddress(orderDelivery.getConsigneeaddress())
               .consigneeTime(orderDelivery.getConsigneetime())
               .consigneeLongitude(orderDelivery.getConsigneelongitude())
               .consigneeLatitude(orderDelivery.getConsigneelatitude())
               .consigneeDetailedAddress(orderDelivery.getConsigneedetailedaddress())
               .consigneeProvince(orderDelivery.getConsigneeprovince())
               .consigneeCity(orderDelivery.getConsigneecity())
               .consigneeCounty(orderDelivery.getConsigneecounty())
               .build();
    }

    /**
     * 根据订单id查询发货方信息收货信息
     * @param orderId 订单id
     * @return 发货方信息收货信息
     */
    public OrderDelivery findByOrderId(Integer orderId){
        OrderDeliveryExample orderDeliveryExample = new OrderDeliveryExample();
        orderDeliveryExample.createCriteria().andOrderidEqualTo(orderId);
        Optional<OrderDelivery> orderDeliveryOne = orderDeliveryMapper.selectByExample(orderDeliveryExample).stream().findFirst();

        OrderDelivery orderDelivery=null;
        if (orderDeliveryOne.isPresent()){
            orderDelivery=orderDeliveryOne.get();
        }
        return orderDelivery;
    }

    /**
     * 插入发货方信息收货信息
     * @param command 入参
     * @param id 订单id
     */
    public void insert(OrderCommand command, Integer id){
        if(Objects.isNull(command.getOrderDeliveryVO())){
            return;
        }
        OrderDelivery orderDelivery = new OrderDelivery();
        orderDelivery.setOrderid(id);
        Optional.ofNullable(command.getOrderDeliveryVO()).ifPresent(od->{
            orderDelivery.setShippercode(command.getOrderDeliveryVO().getShipperCode());
            orderDelivery.setShippername(command.getOrderDeliveryVO().getShipperName());
            orderDelivery.setShippercontact(command.getOrderDeliveryVO().getConsigneeContact());
            orderDelivery.setLoadaddress(command.getOrderDeliveryVO().getLoadAddress());
            orderDelivery.setLoaddetailedaddress(command.getOrderDeliveryVO().getLoadDetailedAddress());
            orderDelivery.setLoadprovince(command.getOrderDeliveryVO().getLoadProvince());
            orderDelivery.setLoadcity(command.getOrderDeliveryVO().getLoadCity());
            orderDelivery.setLoadcounty(command.getOrderDeliveryVO().getLoadCounty());
            orderDelivery.setLoadtime(command.getOrderDeliveryVO().getLoadTime());
            orderDelivery.setLoadlongitude(command.getOrderDeliveryVO().getLoadLongitude());
            orderDelivery.setLoadlatitude(command.getOrderDeliveryVO().getLoadLatitude());
            orderDelivery.setConsigneecode(command.getOrderDeliveryVO().getConsigneeCode());
            orderDelivery.setConsigneename(command.getOrderDeliveryVO().getConsigneeName());
            orderDelivery.setConsigneecontact(command.getOrderDeliveryVO().getConsigneeContact());
            orderDelivery.setConsigneeaddress(command.getOrderDeliveryVO().getConsigneeAddress());
            orderDelivery.setConsigneedetailedaddress(command.getOrderDeliveryVO().getConsigneeDetailedAddress());
            orderDelivery.setConsigneeprovince(command.getOrderDeliveryVO().getConsigneeProvince());
            orderDelivery.setConsigneecity(command.getOrderDeliveryVO().getConsigneeCity());
            orderDelivery.setConsigneecounty(command.getOrderDeliveryVO().getConsigneeCounty());
            orderDelivery.setConsigneetime(command.getOrderDeliveryVO().getConsigneeTime());
            orderDelivery.setConsigneelongitude(command.getOrderDeliveryVO().getConsigneeLongitude());
            orderDelivery.setConsigneelatitude(command.getOrderDeliveryVO().getConsigneeLatitude());
        });
        orderDeliveryMapper.insert(orderDelivery);
    }

    /**
     * 更新发货方信息收货信息
     * @param command 入参
     * @param id 订单id
     */
    public void update(OrderCommand command, Integer id){
        if(Objects.isNull(command.getOrderDeliveryVO())){
            return;
        }
        OrderDelivery orderDelivery = new OrderDelivery();
        orderDelivery.setOrderid(id);
        orderDelivery.setShippercode(command.getOrderDeliveryVO().getShipperCode());
        orderDelivery.setShippername(command.getOrderDeliveryVO().getShipperName());
        orderDelivery.setShippercontact(command.getOrderDeliveryVO().getShipperContact());
        orderDelivery.setLoadaddress(command.getOrderDeliveryVO().getLoadAddress());
        orderDelivery.setLoaddetailedaddress(command.getOrderDeliveryVO().getLoadDetailedAddress());
        orderDelivery.setLoadprovince(command.getOrderDeliveryVO().getLoadProvince());
        orderDelivery.setLoadcity(command.getOrderDeliveryVO().getLoadCity());
        orderDelivery.setLoadcounty(command.getOrderDeliveryVO().getLoadCounty());
        orderDelivery.setLoadtime(command.getOrderDeliveryVO().getLoadTime());
        orderDelivery.setLoadlongitude(command.getOrderDeliveryVO().getLoadLongitude());
        orderDelivery.setLoadlatitude(command.getOrderDeliveryVO().getLoadLatitude());
        orderDelivery.setConsigneecode(command.getOrderDeliveryVO().getConsigneeCode());
        orderDelivery.setConsigneename(command.getOrderDeliveryVO().getConsigneeName());
        orderDelivery.setConsigneecontact(command.getOrderDeliveryVO().getConsigneeContact());
        orderDelivery.setConsigneeaddress(command.getOrderDeliveryVO().getConsigneeAddress());
        orderDelivery.setConsigneedetailedaddress(command.getOrderDeliveryVO().getConsigneeDetailedAddress());
        orderDelivery.setConsigneeprovince(command.getOrderDeliveryVO().getConsigneeProvince());
        orderDelivery.setConsigneecity(command.getOrderDeliveryVO().getConsigneeCity());
        orderDelivery.setConsigneecounty(command.getOrderDeliveryVO().getConsigneeCounty());
        orderDelivery.setConsigneetime(command.getOrderDeliveryVO().getConsigneeTime());
        orderDelivery.setConsigneelongitude(command.getOrderDeliveryVO().getConsigneeLongitude());
        orderDelivery.setConsigneelatitude(command.getOrderDeliveryVO().getConsigneeLatitude());

        OrderDeliveryExample orderDeliveryExample = new OrderDeliveryExample();
        OrderDeliveryExample.Criteria criteria = orderDeliveryExample.createCriteria();
        criteria.andOrderidEqualTo(id);
        orderDeliveryMapper.updateByExampleSelective(orderDelivery,orderDeliveryExample);


    }






}
